package com.drojian.demo3.service.music;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.drojian.demo.R;

public class MusicActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_play,btn_stop,btn_pause,btn_exit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.part3_activity_music);
        btn_play = findViewById(R.id.btn_play);
        btn_stop = findViewById(R.id.btn_stop);
        btn_pause = findViewById(R.id.btn_pause);
        btn_exit = findViewById(R.id.btn_exit);

        btn_play.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, MusicService.class);
        switch (v.getId()){
            case R.id.btn_play:
                intent.putExtra("action","play");
                startService(intent);
                break;
            case R.id.btn_stop:
                intent.putExtra("action","stop");
                startService(intent);
                break;
            case R.id.btn_pause:
                intent.putExtra("action","pause");
                startService(intent);
                break;
            case R.id.btn_exit:
                stopService(intent);
                finish();
                break;
        }
    }
    /**
     * private MediaPlayer player;
     *     private void exitMusic() {
     *         stopMusic();
     *         finish();
     *     }
     *
     *     private void pauseMusic() {
     *         if(player!=null && player.isPlaying()){
     *             player.pause();
     *         }else {
     *             player.start();
     *         }
     *     }
     *
     *     private void stopMusic() {
     *         if(player!=null){
     *             player.stop();
     *             player.reset();//重置
     *             player.release();//释放资源
     *             player = null;
     *             playMusic();
     *         }
     *     }
     *
     *
     *     private void playMusic() {
     *         if(player==null){
     *             player = MediaPlayer.create(this,R.raw.music1);
     *          }
     *         player.start();
     *     }
     */
}